/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaUsergroupUserRel;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class UserGroupRelDao {

	public static void createUsergroupUserRel(SoaUsergroupUserRel user) throws BifangException {
		Tx tx = null;
 
		try {
			tx = TxManager.start();
			
			DaoManager.getDao().save(user);
			DaoManager.getDao().flush();
			tx.commit();
			
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserGroupRelDao.class);
			logger.error("UsergroupUserRelDao.createUsergroupUserRel()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createUsergroupUserRel() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void createUsergroupUserRel(List<SoaUsergroupUserRel> users) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			
			DaoManager.getDao().saveOrUpdateAll(users);
			DaoManager.getDao().flush();
			tx.commit();
			
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserGroupRelDao.class);
			logger.error("UsergroupUserRelDao.createUsergroupUserRel(List)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createUsergroupUserRel(List) error", e);
		} finally {
			tx.end();
		}

	}

	
	
	
	public static void removeUsergroupUserRel(SoaUsergroupUserRel r) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			DaoManager.getDao().delete(r);
			tx.commit();
			
			
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserGroupRelDao.class);
			logger.error("UserGroupRelDao.removeUserGroupRel()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeUsergroupUserRel() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void removeUsergroupUserRel(List<SoaUsergroupUserRel> users) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			
			DaoManager.getDao().deleteAll(users);
			DaoManager.getDao().flush();
			tx.commit();
			
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserGroupRelDao.class);
			logger.error("UsergroupUserRelDao.removeUsergroupUserRel(List)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeUsergroupUserRel(List) error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void removeUsergroupUserRelByUserId(String userId) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaUsergroupUserRel r where r.compId.userId = ?";
			List o = DaoManager.getDao().find(hql,userId);
			
			DaoManager.getDao().deleteAll(o);
			tx.commit();
			
			
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserGroupRelDao.class);
			logger.error("UserGroupRelDao.removeUsergroupUserRelByUserId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeUsergroupUserRelByUserId() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void removeUsergroupUserRelByGroupId(String groupId) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaUsergroupUserRel r where r.compId.usergroupId = ?";
			List o = DaoManager.getDao().find(hql,groupId);
			
			DaoManager.getDao().deleteAll(o);
			tx.commit();
			
			
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserGroupRelDao.class);
			logger.error("UserGroupRelDao.removeUsergroupUserRelByUserId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeUsergroupUserRelByUserId() error", e);
		} finally {
			tx.end();
		}

	}
}
